package com.ly.video.api.user.impl;

import com.ly.video.api.user.entity.InComeEntity;
import com.ly.video.api.user.entity.UserEntity;
import com.ly.video.api.user.entity.WithdrawalEntity;
import com.ly.video.api.user.mapper.InComeMapper;
import com.ly.video.api.user.mapper.UserMapper;
import com.ly.video.api.user.mapper.WithdrawalMapper;
import com.ly.video.api.user.service.IUserService;
import com.ly.video.common.common.Contracts;
import com.ly.video.common.util.WeightUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private InComeMapper inComeMapper;

    @Autowired
    private WithdrawalMapper withdrawalMapper;

    /**
     * 登录逻辑
     * 1. 判断是否存在该用户
     */
    @Override
    public UserEntity login(String openid) {
        // 判断用户是否存在
        UserEntity userByOpenid = userMapper.findUserByOpenid(openid);
        if (userByOpenid != null) {
            return userByOpenid; //判断如果存在即返回用户信息
        }
        // 用户不存在，执行注册逻辑
        Integer result = userMapper.regUserByOpenid(openid);
        if (result != 1) {
            System.out.println("失败");
            return null;
        }
        // 插入成功,获取用户信息
        UserEntity user = userMapper.findUserByOpenid(openid);
        return user;
    }

    @Override
    public UserEntity bindFatherUid(String father, String uid) {
        // 查找师傅用户，用户
        UserEntity fatherEntity = userMapper.findUserByUid(father);
        UserEntity user = userMapper.findUserByUid(uid);
        // 设置用户的师傅及权重
        user.setFather(Integer.valueOf(father));
        user.setWeight(fatherEntity.getWeight() / 2);
        // 修改信息
        Integer i = userMapper.updateUserFatherUid(user);
        if (i != 1) {
            return null;
        }
        return user;
    }

    /**
     * 用户观看激励视频广告
     */
    @Override
    public UserEntity userWatchVideoAdv(String uid) {
        // 设置用户金币数及权重
        UserEntity user = userMapper.findUserByUid(uid);
        // 设置user金币数
        addUserCoin(user.getUid().toString(), user.getWeight(), Contracts.INCOME_CONTENT_WATCH_VIDEO, true);
        // 设置上级金币
        addUserCoin(user.getFather().toString(), user.getWeight() / 5, Contracts.INCOME_CONTENT_SON_WATCH_VIDEO, false);

        return userMapper.findUserByUid(uid);
    }

    /**
     * 通过UID增加用户金币
     */
    @Override
    public void addUserCoin(String uid, Integer coin, String info, boolean isReduceWeight) {
        UserEntity user = userMapper.findUserByUid(uid);
        user.setCoin(user.getCoin() + coin);
        user.setTotalCoin(user.getTotalCoin() + coin);
        if (isReduceWeight) {
            user.setWeight(WeightUtil.watchVideoWeight(user.getWeight()));
            userMapper.updateUserCoinWeight(user);
        } else {
            userMapper.updateUserCoin(user);
        }
        // 添加收入详情
        addInComeInfo(uid, coin, info);
    }

    @Override
    public void userWatchOtherAdv(String uid) {
        UserEntity user = userMapper.findUserByUid(uid);
        user.setWeight(WeightUtil.adsWeight(user.getWeight()));
        userMapper.updateUserWeight(user);
    }

    @Override
    public List<UserEntity> findUserSons(Integer page, Integer size, String uid) {
        List<UserEntity> userSons = userMapper.findUserSons(page, size, uid);
        return userSons;
    }

    @Override
    public List<InComeEntity> findUserInComeInfo(int page, int size, String uid) {
        List<InComeEntity> inComeEntityList = inComeMapper.findUserInComeInfo(page, size, uid);
        return inComeEntityList;
    }

    @Override
    public UserEntity withdrawal(String amount, String uid) {
        UserEntity user = userMapper.findUserByUid(uid);
        Integer coin = (int)(Double.valueOf(amount) * 10000);
        // 判断用户是否具有足够的金币
        if (user.getCoin() < coin) {
            return null;
        }
        // 判断提现金额分为两种情况
        // 1. 小于等于5, 无需审核直接提现
        // 2. 大于5， 需要客服审核
        if (Double.valueOf(amount) <= 5) {
            addWithdrawal(uid, Double.valueOf(amount), 0, Contracts.WITHDRAWAL_SUCCESS);
            // TODO 提现转账
        } else {
            addWithdrawal(uid, Double.valueOf(amount), 1, Contracts.WITHDRAWAL_REVIEW);
        }
        user.setCoin(user.getCoin() - coin);
        // 判断用户is—Ok
        if (user.getIsOk() == 0) {
            // 查询用户是否提现满一块
            List<WithdrawalEntity> userWithdrawalInfo = withdrawalMapper.findUserWithdrawalInfo(0, 2, user.getUid().toString());
            Double totalAmount = 0.0;
            for (WithdrawalEntity withdrawalEntity : userWithdrawalInfo) {
                totalAmount += withdrawalEntity.getAmount();
            }
            if (totalAmount >= 1) {
                // 提现满一块，视为有效用户
                user.setIsOk(1);
                // 上级添加收益
                addUserCoin(user.getFather().toString(), 10000, Contracts.INCOME_CONTENT_SON_NEW_WITHDRAWAL, false);
            }
        }
        // 修改用户金币信息
        userMapper.updateUserCoin(user);
        return user;
    }

    @Override
    public List<WithdrawalEntity> getUserWithdrawalInfo(int page, int size, String uid) {
        List<WithdrawalEntity> userWithdrawalInfo = withdrawalMapper.findUserWithdrawalInfo(page, size, uid);
        return userWithdrawalInfo;
    }

    /**
     * 添加收入信息
     */
    private Integer addInComeInfo(String uid, Integer coin, String content){
        InComeEntity inComeEntity = new InComeEntity();
        inComeEntity.setUid(Integer.valueOf(uid));
        inComeEntity.setCoin(coin);
        inComeEntity.setContent(content);
        inComeEntity.setCreateTime(new Date());
        return inComeMapper.addInComeInfo(inComeEntity);
    }

    /**
     * 添加提现信息
     */
    private Integer addWithdrawal(String uid, Double amount, Integer state, String reason) {
        WithdrawalEntity withdrawalEntity = new WithdrawalEntity();
        withdrawalEntity.setAmount(amount);
        withdrawalEntity.setState(state);
        withdrawalEntity.setReason(reason);
        withdrawalEntity.setUid(Integer.valueOf(uid));
        withdrawalEntity.setCreateTime(new Date());
        Integer withdrawal = withdrawalMapper.withdrawal(withdrawalEntity);
        return withdrawal;
    }

}
